package com.its.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.its.goods.mapper.GoodsMapper;
import com.its.goods.pojo.Goods;
import com.its.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
* @author lenovo
* @description 针对表【gym_goods】的数据库操作Service实现
* @createDate 2025-04-23 15:05:23
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    //依赖注入Mapper层属性
    @Autowired
    private GoodsMapper goodsMapper;

    //新增商品
    @Override
    public int addGoods(Goods goods) {
        return goodsMapper.insert(goods);
    }

    //删除商品
    @Override
    public int deleteGoods(Integer gid) {
        return goodsMapper.deleteById(gid);
    }

    //更新商品信息
    @Override
    public int updateGoods(Goods goods) {
        return goodsMapper.updateById(goods);
    }

    //根据关键字查询商品信息
    @Override
    public List<Goods> selectGoodsByKeyword(String keyword) {
        //校验关键字
        if(StringUtils.isEmpty(keyword)){
            return Collections.emptyList();
        }

        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        keyword = keyword.trim(); // 取出首尾空格
        goodsQueryWrapper.like("goods_name",keyword)
                .or()
                .like("goods_price",keyword)
                .or()
                .like("goods_type",keyword)
                .or()
                .like("goods_inventory",keyword); //根据库存查询商品信息

        return goodsMapper.selectList(goodsQueryWrapper);
    }

    //根据Id查询关键字
    @Override
    public Goods selectGoodsByIdService(Integer gid) {
        return goodsMapper.selectById(gid);
    }

    //分页查询所有商品信息
    @Override
    public Page<Goods> selectAllGoods(Integer page, Integer size) {
        return goodsMapper.selectPage(new Page<>(page,size),null);
    }


    //查询所有商品的信息
    @Override
    public List<Goods> findAllGoods() {
        return goodsMapper.selectList(new QueryWrapper<>());
    }
}




